<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/5/2
 * Time: 17:13
 */
namespace app\home\controller;
use app\data\Data;
use think\Config;
use think\Controller;

/**
 * +-----------------------------------
 * Class Base
 * @package app\home\controller
 * +-----------------------------------
 */
class Base extends Controller{
    /**
     * @return false|\PDOStatement|string|\think\Collection
     * 获取栏目信息
     */
    public function type(){
        $type = new Data('type');
        return $type->selectData('','','order_by asc');
    }

    /**
     * @param string $type_id
     * @return array|false|mixed|\PDOStatement|string|\think\Model
     */
    public function typeSEO($type_id = 0){
        $type = new Data('type');
        return $type->find("id = $type_id");
    }

    /**
     * @param array $param
     * @return mixed
     */
    public function getArticle($param = []){
        $article = new Data('article');
        $where = 'a.id > 0';
        $field = 'a.*,b.type_name,b.id as type_id';
        $order_by = 'create_time desc';
        if(!empty($param['search'])){
            $where .= " and a.title like '%{$param['search']}%'";
        }
        if(!empty($param['type_id'])){
            $where .= " and type_id = {$param['type_id']}";
            $field  = 'a.id,a.title,a.author,a.comment_num,a.laud_num,a.reading_num,a.create_time,a.introduction,b.type_name,b.id as type_id';
        }
        $list = $article->page($where,$order_by,Config::get('paginate.list_rows'),$param,[['type b','a.type_id = b.id','LEFT']],$field,$param['page']);
        return $list;
    }

    /**
     * @return false|\PDOStatement|string|\think\Collection
     * 获取阅读量最多的文章
     */
    public function getReadingArticle(){
        $article = new Data('article');
        return $article->selectData("","id,title,introduction,image","reading_num desc","0,10");
    }

    /**
     * @return false|\PDOStatement|string|\think\Collection
     * 获取置顶文章
     */
    public function getTopArticle(){
        $article = new Data('article');
        return $article->selectData("is_top = 1","id,title,introduction,image","create_time desc","0,10");
    }

    /**
     * @return false|\PDOStatement|string|\think\Collection
     * 获取标签
     */
    public function getTag(){
        $tag = new Data('tag');
        $tag_list = $tag->selectData();
        $tag_list = \think\Helper::countTag($tag_list); //统计每个标签下的文章数量
        return  $tag_list;
    }

    /**
     * @return Data|false|\PDOStatement|string|\think\Collection
     * 获取配置文件
     */
    public function getConfig(){
        $config = new Data('config');
        $config = $config->selectData();
        $config = \think\Helper::config($config);
        return $config;
    }

    /**
     * @param $article_id
     * @return array|false|\PDOStatement|string|\think\Model
     * 获取这条文章数据
     */
    public function getArticleOne($article_id){
        $article = new Data('article');
        return $article->joinOne([["type b","a.type_id = b.id","LEFT"]],"a.id = $article_id","a.*,b.type_name,b.id as type_id");
    }

    /**
     * @param $article_id
     * @return Data|mixed
     */
    public function getArticleTag($article_id){
        $article_tag = new Data('article_tag');
        $article_tag = $article_tag->joinData("a",[['tag b','a.tag_id = b.id','LEFT']],"a.article_id = $article_id","b.id,b.tag_name,a.tag_id");
        return $article_tag;
    }

    /**
     * @param array $tag_id
     * @param $article_id
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getRelatedArticle($tag_id = [],$article_id = 0){
        $article_tag = new Data('article_tag');
        $where = \think\Helper::relatedWhere($tag_id,$article_id);
        return $article_tag->distinct('b.title','a',[['article b','a.article_id = b.id','LEFT']],$where,'b.title,b.id,b.create_time',"b.id desc","0,10");
    }

    public function getTagArticle($get = []){
        $article_tag = new Data('article_tag');
        $where = "a.tag_id = {$get['tag_id']}";
        return $article_tag->page($where,"create_time desc",Config::get('paginate.list_rows'),$where,[['article b','a.article_id = b.id','LEFT']],"b.*",$get['page']);
    }

    /**
     * @param int $tag_id
     * @return array|false|mixed|\PDOStatement|string|\think\Model
     * 获取一条标签名称
     */
    public function getTagOne($tag_id = 0){
        $tag = new Data('tag');
        return $tag->find("id = $tag_id");
    }

    /**
     * @return false|\PDOStatement|string|\think\Collection
     * 获取友情连接
     */
    public function link(){
        $link = new Data('link');
        return $link->selectData('status = 1','id,link_name,link_url');
    }

    /**
     * @param int $article_id
     * @return \app\data\mixeds
     * 增加阅读量
     */
    public function _incReading($article_id = 0){
        $article = new Data('article');
        return $article->_setInc("id = $article_id",'reading_num',1);
    }

    /**
     * @param $get
     * @return \think\Paginator
     */
    public function getProduct(){
        $product = new Data('product');
        return $product->pageSelf();
    }
}